package com.wing.sys.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Code;
import com.wing.sys.service.CodeManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/sys/code")
public class CodeController {

	@Autowired
	private CodeManager codeManager;
	
	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/code/codeIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String get(@PathVariable String url) {
		return "sys/code/"+url;
	}
	
	/**
	 * 保存一个编码
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public @ResponseBody String save(Code code,HttpServletRequest request) {
		String ptree_id = request.getParameter("ptree_id");
		codeManager.saveCode(code,ptree_id);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}

	/**
	 * 打开编码编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEdit/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable
	String id, Model model,HttpServletRequest request) {
		Code code = new Code();
		Code pcode = null;
		if(!id.equals("0")){
			code = codeManager.findById(id);
			pcode = codeManager.findById(code.getParent_id());
		}else{
			String pcodeId = request.getParameter("pcodeId");
			code.setParent_id(pcodeId);
			pcode = codeManager.findById(pcodeId);
		}
		model.addAttribute("data", code);
		model.addAttribute("pdata", pcode);
		return "sys/code/codeForm";
	}

	/**
	 * 删除编码
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "delcodes", method = RequestMethod.POST)
	public @ResponseBody String delcodes(HttpServletRequest request) throws IOException {
		String chk [] = request.getParameterValues("chk");
		int count = codeManager.deleteCodes(chk);
		return ""+count;
	}
	
	/**
	 * ajax方式 获取一个编码列表 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage,
			String pageSize,String codeId,HttpServletRequest request) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  codeManager.findPageList(currentPage, _iPageSize,codeId);
	}
	
	/**
	 * ajax方式 获取一个编码列表 
	 * @return
	 */
	@RequestMapping(value = "getCodes", method = RequestMethod.POST)
	public @ResponseBody List getCodes(String code) {
		return  codeManager.findChildByCode(code);
	}
	
	/**
	 * 是根据编码parentId获取的子机构,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 *  
	 * @throws ServletException, IOException
	 */
	@RequestMapping(value = "showTree", method = RequestMethod.GET)
	public void showTree(HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");//HTTP 1.1    
		response.setHeader("Cache-Control", "no-cache");//HTTP 1.0    
		response.setHeader("Expires", "0");//防止被proxy
		
		String pid = request.getParameter("id");
		String code = request.getParameter("code");
		boolean firstFlag = false;
		if (pid == null || pid.equalsIgnoreCase("")) {
			Code rootcode = codeManager.findByCode(code);
			pid = Code.CODEROOT;//我数据库中一级节点的parentid都是-1
			if(rootcode != null){
				pid = rootcode.getId();
			}
			firstFlag = true;
		}
		try {
			//根据上一级节点,查询该节点下的所有节点 
//			String aa = codeManager.createDHTMLXML(pid,firstFlag,domainId);
			response.getWriter()
					.write(codeManager.createDHTMLXML(pid,firstFlag));
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 验证code是否已被占用  我的code
	 * @param id
	 * @param model
	 * @return*/
	
	@RequestMapping(value = "vlcode", method = RequestMethod.GET)
	public @ResponseBody String vlcode(String code,String codeId) {
		List list = codeManager.findCodesByCode(code);
		if(list.size()==0){
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		if(list.size()>1){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Map map = (Map)list.get(0);
		if(!map.get("id").equals(codeId)){
			return Constants.DEFAULT_AJAX_FAILURE;
		}else{
			return Constants.DEFAULT_AJAX_SUCCESS;
		}
		
	}
}

